/*
 * @Author: wuqingchun
 * @Date: 2024-09-24 14:42:21
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-10-03 21:09:52
 */
#ifndef __CHAT_SESSION_H__
#define __CHAT_SESSION_H__

#include <cstdint>
#include <array>
#include <unordered_map>
#include <mutex>

#include "chat_msg.h"

#include "chat_net.h"
#include "model/chat_user_model.h"
#include "redis/chat_redis_subpub.h"


using SessionEventCallback = std::function<void(const TcpConnectionPtr &refConnPtr, const CHAT_MSG_HEAD_S& refMsgHead, Timestamp time)>;

class CChatSession
{
public:
    CChatSession();

    int32_t Init();
    
    /**
     * @brief 用户注册
     * 
     * @param pcName 
     * @param pcPassword 
     * @return int32_t 
     */
    int32_t RegisterUser(const char* pcName, const char* pcPassword);
    void OnConnect(const TcpConnectionPtr& refConnPtr);
    void OnMessage(const TcpConnectionPtr& refConnPtr, Buffer* pobjBuf, Timestamp objTS);
private:

    void __SendSetResponse(const TcpConnectionPtr& refConnPtr, int iMsgType, int iErrCode);
    void __DoLogin(const TcpConnectionPtr &refConnPtr, const CHAT_MSG_HEAD_S& refMsgHead, Timestamp time);
    void __DoLogut(const TcpConnectionPtr &refConnPtr, const CHAT_MSG_HEAD_S& refMsgHead, Timestamp time);
    void __DoRegister(const TcpConnectionPtr &refConnPtr, const CHAT_MSG_HEAD_S& refMsgHead, Timestamp time);
    void __DoChat(const TcpConnectionPtr &refConnPtr, const CHAT_MSG_HEAD_S& refMsgHead, Timestamp time);
private:
    CChatUserModel m_objUserModel;
    std::array<SessionEventCallback, CHAT_MSG_MAX> m_arrMsgHandlers {};
    std::unordered_map<int, TcpConnectionPtr> m_mapUserConn;
    std::mutex m_objConnMtx;
    CChatRedisSubPub m_objSubPub;
    CChatRedisSubPub m_objPublisher;
};
#endif //__CHAT_SESSION_H__